package com.wr.utils;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * @ClassName: EhcacheUtils
 * @Description: 缓存工具类
 * @author 吴睿
 * @date 2016-11-29
 */
public class EhcacheUtils {
    private static CacheManager manager = CacheManager.create();

    /**
     * 获取缓存
     * @param cacheName 缓存名字
     * @param key   缓存的key
     * @return  返回结果
     */
    public static Object get(String cacheName, Object key) {
        Cache cache = manager.getCache(cacheName);
        if (cache != null) {
            Element element = cache.get(key);
            if (element != null) {
                return element.getObjectValue();
            }
        }
        return null;
    }

    /**
     * 将数据缓存
     * @param cacheName 缓存名字
     * @param key   缓存key
     * @param value 数据
     */
    public static void put(String cacheName, Object key, Object value) {
        Cache cache = manager.getCache(cacheName);
        if (cache != null) {
            cache.put(new Element(key, value));
        }
    }

    /**
     * 移除缓存
     * @param cacheName 缓存名字
     * @param key   缓存key
     * @return  返回结果
     */
    public static boolean remove(String cacheName, Object key) {
        Cache cache = manager.getCache(cacheName);
        if (cache != null) {
            return cache.remove(key);
        }
        return false;
    }
  
  
}  